/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.domain.MOIDRelation;
import com.carnation.mong.platform.domain.MOIData;
import com.carnation.mong.platform.domain.MOIRelationType;
import com.carnation.mong.platform.service.DRelationService;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class DRelationServiceImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private DRelationService service;

	public DRelationServiceImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(DRelationService.class);
	}

	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("create".equals(method)) {
			returnStr = create(item);
		} else if ("delete".equals(method)) {
			returnStr = delete(item);
		} else if ("getDRelation".equals(method)) {
			returnStr = getDRelation(item);
		} else if ("update".equals(method)) {
			returnStr = update(item);
		} else {
			throw new IllegalArgumentException(method);
		}
		return returnStr;
	}

	private String create(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			MOIDRelation instance = instanceMOIDRelation(item.getChildren()
					.get(0));
			if (log.isDebugEnabled()) {
				log.debug("MOIDRelation:" + instance);
			}
			Object obj = null;
			try {
				obj = service.create(instance);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String delete(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			MOIDRelation instance = instanceMOIDRelation(item.getChildren()
					.get(0));
			if (log.isDebugEnabled()) {
				log.debug("MOIDRelation:" + instance);
			}
			Object obj = null;
			try {
				service.delete(instance);
				obj = true;
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getDRelation(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			String id = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("id:" + id);
			}
			Object obj = null;
			try {
				obj = service.getDRelation(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		} else if (item.getChildren().size() == 3) {
			MOIData srcData = instanceMOIData(item.getChildren().get(0));
			MOIData tarData = instanceMOIData(item.getChildren().get(1));
			MOIRelationType rt = instanceMOIRelationType(item.getChildren()
					.get(2));
			if (log.isDebugEnabled()) {
				log.debug("getDRelation srcData:" + srcData + ",tarData:"
						+ tarData + ",rt:" + rt);
			}
			Object obj = null;
			try {
				obj = service.getDRelation(srcData, tarData, rt);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String update(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			MOIDRelation instance = instanceMOIDRelation(item.getChildren()
					.get(0));
			if (log.isDebugEnabled()) {
				log.debug("MOIDRelation:" + instance);
			}
			Object obj = null;
			try {
				service.update(instance);
				obj = service.getDRelation(instance.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

}
